// Copyright 2013 Vedran Vuk. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"fmt"
	"net/http"
	"os"
	"os/exec"
	"runtime"
)

func runIndex(file string) {
	f := "http://localhost:" + os.Args[1] + "/" + file
	
	var err error
	switch runtime.GOOS {
	case "linux":
	    err = exec.Command("xdg-open", f).Start()
	case "windows", "darwin":
	    err = exec.Command("rundll32.exe", "url.dll,FileProtocolHandler", f).Start()
	default:
	    err = fmt.Errorf("unsupported platform")
	}	
	
	if err != nil {
		fmt.Println(err)
	}
}

func main() {
	if len(os.Args) < 2 || len(os.Args) > 3 {
		fmt.Println("Usage:\ngoserve <port> <path>")
		return
	}
	
	runIndex("index.html")

	if err := http.ListenAndServe(":"+os.Args[1], http.FileServer(http.Dir(os.Args[2]))); err != nil {
		fmt.Println("ListenAndServe failed: ", err)
		return
	}
}
